package cn.wolfcode.interceptor;


import cn.wolfcode.domain.Result;
import cn.wolfcode.utils.JWTUtils;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //添加拦截器

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String token = request.getHeader("token");
        try{
            JWTUtils.parseToken(token);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            Result error = Result.error("NOT_LOGIN");
            response.setContentType("application/json;charset=utf-8");
            String json= JSON.toJSONString(error);
            response.getWriter().write(json);
            return false;
        }
    }



}
